<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-card class="search-card" shadow="never">
      <!-- 搜索操作 -->
      <template #header>
        <div class="card-header">
          <span>筛选条件</span>
          <el-button class="collapse-btn" type="text" @click="toggleSearch"
            :icon="showSearch ? 'ArrowDownBold' : 'ArrowUpBold'">
            {{ showSearch ? '收起' : '展开' }}
          </el-button>
        </div>
      </template>

      <!-- 表单 -->
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="75px">
        <!-- <el-form-item label="关联支付ID" prop="paymentId">
        <el-input
          v-model="queryParams.paymentId"
          placeholder="请输入关联支付ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
        <el-form-item label="订单编号" prop="orderId">
          <el-input v-model="queryParams.orderId" placeholder="请输入订单编号" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <!-- <el-form-item label="退款金额" prop="refundAmt">
        <el-input
          v-model="queryParams.refundAmt"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
        <el-form-item label="退款状态" prop="refundStatus">
          <el-select v-model="queryParams.refundStatus" placeholder="请选择退款状态" clearable>
            <el-option v-for="dict in refund_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="退款原因" prop="refundReason">
        <el-select v-model="queryParams.refundReason" placeholder="请选择退款原因" clearable>
          <el-option
            v-for="dict in refund_reason"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
        <el-form-item label="微信退款交易单号" label-width="130" prop="refundTransId">
          <el-input v-model="queryParams.refundTransId" placeholder="请输入微信退款交易单号" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker clearable v-model="queryParams.refundTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择退款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option v-for="dict in wdm_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
        <el-form-item label="退款原因" prop="reason">
          <el-select v-model="queryParams.reason" placeholder="请选择退款原因" clearable>
            <el-option v-for="dict in refund_reason" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="退款单号" prop="refundCode">
          <el-input v-model="queryParams.refundCode" placeholder="请输入退款单号" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

    </el-card>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="operation-buttons">
      <el-col :span="24">
        <el-card class="operation-card" shadow="never">
          <el-row :gutter="10" align="middle" class="operation-content">
            <el-col :span="6">
              <div class="operation-title">
                <el-icon name="Document" style="margin-right: 8px; color: #409eff;" />
                <h3>退款信息操作</h3>
                <el-tag type="info" class="total-count">共 {{ total }} 条记录</el-tag>
              </div>
            </el-col>

            <el-col :span="18" class="operation-actions">
              <el-button-group class="action-group">
                <!-- <el-button type="primary" plain icon="Plus" @click="handleAdd"
                  v-hasPermi="['ruoyi:refundInfo:add']">新增</el-button>
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                  v-hasPermi="['ruoyi:refundInfo:edit']">修改</el-button> -->
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                  v-hasPermi="['ruoyi:refundInfo:remove']">删除</el-button>
                <el-button type="warning" plain icon="Download" @click="handleExport"
                  v-hasPermi="['ruoyi:refundInfo:export']">导出</el-button>
              </el-button-group>
              <right-toolbar class="search-toolbar" v-model:showSearch="showSearch"
                @queryTable="getList"></right-toolbar>

            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>

    <!-- 表单渲染 -->
    <el-card class="table-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span>退款信息列表</span>
          <div class="header-tools">
            <el-button type="text" icon="Refresh" @click="getList">刷新</el-button>
            <el-popover placement="bottom" :width="100" trigger="click">
              <template #reference>
                <el-button type="text" icon="Setting">列设置</el-button>
              </template>
              <div class="column-settings">
                <el-checkbox-group v-model="visibleColumns">
                  <el-checkbox v-for="col in allColumns" :key="col.prop" :label="col.prop">
                    {{ col.label }}
                  </el-checkbox>
                </el-checkbox-group>
              </div>
            </el-popover>
          </div>
        </div>
      </template>

      <!-- 表格 -->
      <el-table v-loading="loading" :data="refundInfoList" border resizble @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" width="60" align="center" prop="refundId" />
        <!-- <el-table-column label="关联支付ID" align="center" prop="paymentId" /> -->
        <el-table-column label="退款单号" align="center" min-width="240" prop="refundCode" />
        <el-table-column label="订单编号" align="center" prop="orderId" />

        <!-- <el-table-column label="退款金额" align="center" prop="refundAmt" /> -->
        <el-table-column prop="refundAmt" align="center" label="退款金额">
          <template #default="scope">
            {{ formatAmount(scope.row.refundAmt) }}
          </template>
        </el-table-column>


        <el-table-column label="退款状态" align="center" prop="refundStatus">
          <template #default="scope">
            <dict-tag :options="refund_status" :value="scope.row.refundStatus" />
          </template>
        </el-table-column>
        <el-table-column label="退款原因" align="center" min-width="150" prop="reason">
          <template #default="scope">
            <dict-tag :options="refund_reason" :value="scope.row.reason" />
          </template>
        </el-table-column>

        <!-- <el-table-column label="退款原因" align="center" prop="refundReason">
        <template #default="scope">
          <dict-tag :options="refund_reason" :value="scope.row.refundReason"/>
        </template>
      </el-table-column> -->
        <!-- <el-table-column
        label="微信退款交易单号"
        align="center"
        min-width="280"
        prop="refundTransId"
      /> -->
        <el-table-column label="退款时间" align="center" prop="refundTime" width="240">
          <template #default="scope">
            <span>{{
              parseTime(scope.row.refundTime, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="wdm_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="版本号" align="center" prop="version" /> -->
        <!-- <el-table-column label="备注信息" align="center" prop="remark" /> -->

        <el-table-column label="操作" align="center" min-width="150" class-name="small-padding fixed-width">
          <template #default="scope">
            <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ruoyi:refundInfo:edit']">修改</el-button> -->
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['ruoyi:refundInfo:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改退款信息日志管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="refundInfoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联支付ID" prop="paymentId">
          <el-input v-model="form.paymentId" placeholder="请输入关联支付ID" />
        </el-form-item>
        <el-form-item label="关联订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入关联订单ID" />
        </el-form-item>

        <!-- <el-form-item label="退款金额" prop="refundAmt">
          <el-input v-model="form.refundAmt" placeholder="请输入退款金额" />
        </el-form-item> -->
        <el-form-item label="金额(元)">
          <el-input v-model="form.amount" placeholder="请输入金额" @input="handleAmountInput">
            <template #append>元</template>
          </el-input>
        </el-form-item>

        <el-form-item label="退款状态" prop="refundStatus">
          <el-select v-model="form.refundStatus" placeholder="请选择退款状态">
            <el-option v-for="dict in refund_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款原因" prop="refundReason">
          <el-select v-model="form.refundReason" placeholder="请选择退款原因">
            <el-option v-for="dict in refund_reason" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="微信退款交易单号" prop="refundTransId">
          <el-input v-model="form.refundTransId" placeholder="请输入微信退款交易单号" />
        </el-form-item>
        <el-form-item label="退款成功时间" prop="refundTime">
          <el-date-picker clearable v-model="form.refundTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择退款成功时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in wdm_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio v-for="dict in del_flag" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="退款原因" prop="reason">
          <el-select v-model="form.reason" placeholder="请选择退款原因">
            <el-option v-for="dict in refund_reason" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款单号" prop="refundCode">
          <el-input v-model="form.refundCode" placeholder="请输入退款单号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="RefundInfo">
import {
  listRefundInfo,
  getRefundInfo,
  delRefundInfo,
  addRefundInfo,
  updateRefundInfo,
} from "@/api/ruoyi/refundInfo";

const { proxy } = getCurrentInstance();

const { refund_status, wdm_status, refund_reason, del_flag } = proxy.useDict(
  "refund_status",
  "wdm_status",
  "refund_reason",
  "del_flag"
);

const refundInfoList = ref([]);

const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    paymentId: null,
    orderId: null,
    refundAmt: null,
    refundStatus: null,
    refundReason: null,
    refundTransId: null,
    refundTime: null,
    status: null,
    version: null,
    reason: null,
    refundCode: null,
  },
  rules: {
    paymentId: [
      { required: true, message: "关联支付ID不能为空", trigger: "blur" },
    ],
    orderId: [
      { required: true, message: "关联订单ID不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

// 新增：搜索区域折叠/展开
function toggleSearch() {
  showSearch.value = !showSearch.value;
}

/** 查询退款信息日志管理列表 */
function getList() {
  loading.value = true;
  listRefundInfo(queryParams.value).then((response) => {
    // refundInfoList.value = response.rows
    // 过滤已删除数据（假设 delFlag 字段标识删除状态）
    refundInfoList.value = response.rows.filter(
      (item) => item.delFlag !== "2"
    );
    total.value = response.total;
    loading.value = false;
  });
}

// 格式化金额（分转元）
function formatAmount(value) {
  if (!value && value !== 0) return "--";
  return (value / 100).toFixed(2);
}

// 输入处理（元转分）
function handleAmountInput(value) {
  // 移除非数字和小数点字符
  const numericValue = value.replace(/[^0-9.]/g, "");

  // 分割整数和小数部分
  const parts = numericValue.split(".");

  // 保留两位小数
  if (parts[1] && parts[1].length > 2) {
    parts[1] = parts[1].slice(0, 2);
  }

  // 组合并转换为分
  const formattedValue = parts[0] + (parts[1] ? `.${parts[1]}` : "");
  const cents = Math.round(parseFloat(formattedValue || "0") * 100);

  // 更新表单值（存储为分）
  form.value.amount = cents;
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    refundId: null,
    paymentId: null,
    orderId: null,
    refundAmt: null,
    refundStatus: null,
    refundReason: null,
    refundTransId: null,
    refundTime: null,
    status: null,
    delFlag: null,
    version: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
    reason: null,
    refundCode: null,
  };
  proxy.resetForm("refundInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.refundId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加退款信息日志管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _refundId = row.refundId || ids.value;
  getRefundInfo(_refundId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改退款信息日志管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["refundInfoRef"].validate((valid) => {
    if (valid) {
      if (form.value.refundId != null) {
        updateRefundInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRefundInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _refundIds = row.refundId || ids.value;
  proxy.$modal
    .confirm('是否确认删除退款信息日志管理编号为"' + _refundIds + '"的数据项？')
    .then(function () {
      return delRefundInfo(_refundIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi/refundInfo/export",
    {
      ...queryParams.value,
    },
    `refundInfo_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>


<style scoped>
/* 整体布局优化 */
.app-container {
  padding: 16px;
  background-color: #f5f7fa;
}

/* 金额显示样式 */
.amount-display {
  font-family: monospace;
  color: #1890ff;
}

/* 操作栏卡片样式 */
.operation-card {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  margin: 8px 0;
}

.operation-content {
  display: flex;
  align-items: center;
  padding: 12px 15px;
}

/* 搜索卡片样式 */
.search-card {
  /* margin-bottom: 16px; */
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
  color: #303133;
}

.collapse-btn {
  padding: 0;
  font-size: 14px;
  color: #606266;
}

.search-form {
  padding: 0 10px;
}

.search-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
  padding-bottom: 10px;
}

.advanced-toggle {
  margin-left: 10px;
  padding: 0;
  font-size: 14px;
}

.advanced-search {
  background-color: #f8f9fa;
  border-radius: 4px;
  padding: 15px;
  margin-top: 15px;
  border: 1px solid #ebeef5;
}

.full-width {
  width: 100%;
}

/* 操作栏样式 */
.operation-bar {
  margin-bottom: 16px;
  /* align-items: center; */
}

.operation-title {
  display: flex;
  align-items: center;
}

.operation-title h3 {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0;
  line-height: 1;
}

.operation-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.total-count {
  margin-left: 10px;
  font-size: 12px;
  height: 20px;
  line-height: 20px;
  padding: 0 6px;
}

.operation-buttons {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.operation-buttons .el-button-group {
  margin-right: 10px;
}

/* 表格卡片样式 */
.table-card {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.header-tools {
  display: flex;
  gap: 10px;
}

.column-settings {
  padding: 10px;
}

/* 表格样式优化 */
.refund-table {
  border-radius: 4px;
}

.amount {
  color: #F56C6C;
  font-weight: bold;
}

.time-cell {
  display: flex;
  align-items: center;
  color: #606266;
}

.time-cell .el-icon {
  margin-right: 5px;
  color: #909399;
}

.action-group {
  /* display: flex;
  gap: 5px; */
  margin-right: 10px;
}

.search-toolbar {
  display: inline-flex;
  align-items: center;
}

.pagination {
  margin-top: 20px;
  justify-content: center;
}

/* 对话框样式 */
.refund-dialog {
  border-radius: 10px;
}

.dialog-header {
  text-align: center;
  padding-bottom: 15px;
}

.dialog-header h3 {
  font-size: 20px;
  color: #303133;
  margin-bottom: 8px;
}

.header-divider {
  height: 2px;
  width: 50px;
  background-color: #409eff;
  margin: 0 auto;
  border-radius: 1px;
}

.refund-form {
  padding: 10px 20px;
}

.system-info {
  background-color: #f8f9fa;
  border-radius: 4px;
  padding: 15px;
}

.info-item {
  display: flex;
  margin-bottom: 10px;
  font-size: 14px;
  color: #606266;
}

.info-item .label {
  width: 80px;
  font-weight: bold;
  color: #303133;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  padding: 15px 0;
  border-top: 1px solid #ebeef5;
}
</style>
